package com.scrcu.core.batch.biz;

import com.boarsoft.common.Util;
import com.boarsoft.common.dao.PagedResult;
import com.scrcu.core.batch.constant.BatchConstants;
import com.scrcu.core.batch.dao.SqlSessionDao;
import com.scrcu.core.batch.entity.BatchExecution;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Component
public class BatchExecutionBizImpl extends SqlSessionDao implements BatchExecutionBiz {

    public BatchExecutionBizImpl(SqlSession dao) {
        super(dao);
    }

    private final String namespace = "com.scrcu.core.batch.BatchExecution.";

    /**
     * 插入一条新的任务组执行记录
     *
     * @return 主键
     */
    @Override
    @Transactional
    public String save(BatchExecution o) {
        if (Util.strIsEmpty(o.getId())) {
            dao.insert(namespace.concat("insertSelective"), o);
        } else {
            dao.update(namespace.concat("updateSelective"), o);
        }
        return o.getId();
    }

    /**
     * 根据任务组ID获取最后的一次执行记录。目的是检查有无正在执行的记录。
     *
     */
    @Override
    @Transactional(readOnly = true)
    public BatchExecution last(String infoId) {
        return dao.selectOne(namespace.concat("selectRecentOneByInfoId"), infoId);
    }

    @Override
    @Transactional(readOnly = true)
    public BatchExecution get(String id) {
        return dao.selectOne(namespace.concat("selectById"), id);
    }

    /**
     * 根据id数组查询执行记录
     *
     */
    @Override
    @Transactional(readOnly = true)
    public List<BatchExecution> list(String[] ids) {
        List<String> idList = new ArrayList<>(Arrays.asList(ids));
        return dao.selectList(namespace.concat("selectByIdList"), idList);
    }

    @Override
    @Transactional(readOnly = true)
    public List<BatchExecution> list() {
        List statusList = Collections.singletonList(new short[]{BatchConstants.EXECUTION_STATUS_NEW,
                BatchConstants.EXECUTION_STATUS_QUEUED,
                BatchConstants.EXECUTION_STATUS_RUNNING,
                BatchConstants.EXECUTION_STATUS_SUSPEND,
                BatchConstants.EXECUTION_STATUS_STOPPING}
        );
        return dao.selectList(namespace.concat("selectByStatusList"), statusList);
    }

    @Override
    @Transactional(readOnly = true)
    public PagedResult<BatchExecution> list(String catalog, String key, String startTime, String endTime, int pageNo,
                                            int pageSize) {
        Map param = new HashMap<>();
        param.put("catalog", catalog);
        param.put("key", "%".concat(key).concat("%"));//模糊查询在这里拼接，防止sql注入
        param.put("startTime", startTime);
        param.put("endTime", endTime);
        param.put("start", pageNo * pageSize);
        param.put("pageSize", pageSize);
        int total = dao.selectOne(namespace.concat("selectCountBySelective"), param);
        List<BatchExecution> lt = dao.selectList(namespace.concat("selectBySelective"), param);
        return new PagedResult<>(total, lt, pageNo, pageSize);
    }

    @Override
    @Transactional(readOnly = true)
    public PagedResult<BatchExecution> list(String infoId, String startTime, String endTime, int pageNo, int pageSize) {
        Map param = new HashMap<>();
        param.put("infoId", infoId);
        param.put("startTime", startTime);
        param.put("endTime", endTime);
        param.put("start", pageNo * pageSize);
        param.put("pageSize", pageSize);
        int total = dao.selectOne(namespace.concat("selectCountBySelective"), param);
        List<BatchExecution> lt = dao.selectList(namespace.concat("selectBySelective"), param);
        return new PagedResult<>(total, lt, pageNo, pageSize);
    }

    @Override
    @Transactional(readOnly = true)
    public List<BatchExecution> list(short status) {
        Map param = new HashMap<>();
        param.put("status", status);
        return dao.selectList(namespace.concat("selectBySelective"), param);
    }

    @Override
    @Transactional(readOnly = true)
    public PagedResult<Map<String, Object>> listExecuting(int pageNo, int pageSize) {
        Map param = new HashMap<>();
        param.put("start", pageNo * pageSize);
        param.put("pageSize", pageSize);
        int total = dao.selectOne(namespace.concat("selectExectingCount"));
        List<Map<String, Object>> lt = dao.selectList(namespace.concat("selectExectingForList"), param);
        return new PagedResult<>(total, lt, pageNo, pageSize);
    }

    @Override
    @Transactional
    public void delete(String id) {
        // 删除执行记录的相关事件
        dao.delete("com.scrcu.core.batch.BatchEvent.deleteByExeId",id);
        // 删除执行记录
        dao.delete(namespace.concat("deleteById"), id);
    }

    @Override
    @Transactional
    public int updateStatusSuccess(String id) {
        this.dao.update(namespace.concat("updateStatusSuccess"),id);
        return 0;
    }

    @Override
    @Transactional
    public int updateStatusFailed(String id) {
        this.dao.update(namespace.concat("updateStatusFailed"),id);
        return 0;
    }

}